package org.Lin.controller;

import org.json.JSONObject;

import java.io.*;
import java.net.Socket;

public class MainServerController {

    private final LoginController loginController = new LoginController();
    private final RegisterController registerController = new RegisterController();
    private final ChatRoomMessageController chatRoomMessageController = new ChatRoomMessageController();
    private final FriendMessageController friendMessageController = new FriendMessageController();
    private final AdminController adminController = new AdminController();
    private final MessageQueryController messageQueryController = new MessageQueryController();

    public void handleRequest(JSONObject request, Socket socket) {
        String type = request.getString("type");
        try {
            switch (type) {
                case "login":
                    loginController.handleLogin(request, socket);
                    break;
                case "register":
                    registerController.handleRegister(request, socket);
                    break;
                case "chatroom_message":
                    chatRoomMessageController.handleSendMessage(request, socket);
                    break;
                case "friend_message":
                    friendMessageController.handleSendPrivateMessage(request, socket);
                    break;
                case "admin_operation":
                    adminController.handleAdminOperation(request, socket);
                    break;
                case "query_messages":
                    messageQueryController.handleQueryMessages(request, socket);
                    break;
                default:
                    JSONObject error = new JSONObject();
                    error.put("status", "error");
                    error.put("message", "未知请求类型");
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                    out.println(error.toString());
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
